//显示基本信息
var basicInfomation = document.getElementById("basicInfomation").children[0];
var basicInfomationData = [
    ["委托单位", "项目名称","产品名称","检测部位", "检测时机", "焊接方法", "坡口形式", "设备型号", "焦点尺寸", "像质计型号",
        "增感方式", "胶片牌号", "胶片规格", "显影时间", "洗片条件", "标准黑度范围", "检测标准", "合格级别", "热处理状态",
        "材质", "设备编号", "显影液", "显影温度", "技术等级", "验收标准", "检测比例", "编制人员级别", "备注"],
    []];
//, "产品名称"
//,"task_product"
var inputID = [
    "r_company", "r_pname","task_product","r_position", "r_opportunity", "r_weldM", "r_type",
    "r_equ", "r_focus", "r_IQIV", "r_sensitizer", "r_filmBrand", "r_filmV",
    "r_devTime", "r_devCondition", "r_b", "r_standard", "r_qua", "r_heat",
    "r_texture", "r_equid", "r_develop", "r_devT", "r_tlevel", "r_check", "r_scale", "r_level", "r_remark"
];
$.ajax({
    url: '/echo/report',
    type: 'post',
    data: {
        "requestType": "crudeBaseData",
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            console.log(data)
            console.log(data[1])
            let tempData = [];
            let elementName = [
                "pj_name", "pj_company", "task_position", "task_opportunity", "op_weldM", "w_type", "tech_focus",
                "op_sensitizer", "op_filmBrand", "op_filmV", "tech_devTime", "tech_devCondition", "task_standard", "w_qua",
                "task_heat", "op_texture", "op_equid", "op_liquor", "tech_devT", "task_level", "task_check", "op_scale",
                "op_equ", "tech_IQIV", "min", "max","task_product"
            ];
            if (data[0].length > 1 || data[1].length > 1 || data[12] > 1 || data[13] > 1) {
                alert("你选择的工件无法生成报告，请检查。");
                history.back();
            }
            for (let i = 0; i < data.length; i++) {
                tempData[i] = [];
                for (let j = 0; j < data[i].length; j++) {
                    tempData[i].push(data[i][j][elementName[i]]);
                }
            }
            basicInfomationData[1] = [
                tempData[1], tempData[0],tempData[26], tempData[2], tempData[3], tempData[4], tempData[5], tempData[22], tempData[6],
                tempData[23], tempData[7], tempData[8], tempData[9], tempData[10], tempData[11], [tempData[24][0].toFixed(3) + "-" + tempData[25][0].toFixed(3)], tempData[12],
                tempData[13], tempData[14], tempData[15], tempData[16], tempData[17], tempData[18], tempData[19],
                tempData[20], tempData[21], [""], [""]
            ];
            console.log("yo")
            console.log(tempData)
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});

for (let i = 0; i < basicInfomationData[0].length; i++) {
    let tr = document.createElement("tr");
    if (i % 3 === 0) {
        basicInfomation.appendChild(tr);
    }
    let td = document.createElement("td");
    td.setAttribute("class", "td-shadows");
    td.setAttribute("align", "center");
    td.innerHTML = basicInfomationData[0][i];
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);
    td = document.createElement("td");
    td.setAttribute("align", "center");
    let input = document.createElement("input");
    input.setAttribute("list", "list" + i);
    input.setAttribute("id", inputID[i]);
    if (basicInfomationData[1][i].length === 1)
        input.value = basicInfomationData[1][i][0];
    td.appendChild(input);
    let list = document.createElement("datalist");
    list.setAttribute("id", "list" + i);
    for (let j = 0; j < basicInfomationData[1][i].length; j++) {
        let option = document.createElement("option");
        option.setAttribute("value", basicInfomationData[1][i][j]);
        list.appendChild(option);
    }
    td.appendChild(list);
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);
}

//显示工艺信息
var tecTable = document.getElementById("tecTable");
var tecTitle = ["工艺卡编号", "工艺规程/版本", "规格", "透照方式", "像质计灵敏度",
    "焦距", "一次透照长度", "分段数", "实拍数", "管电压", "管电流/源活", "曝光时间"];
var tecInfomation = [];
$.ajax({
    url: '/echo/report',
    type: 'post',
    data: {
        "requestType": "techData",
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            console.log(data)
            for (let i = 0; i < data.length; i++) {
                tecInfomation.push([
                    data[i].tech_id,
                    data[i].tech_version,
                    data[i].w_spec,
                    data[i].tech_tranType,
                    data[i].tech_IQISen,
                    data[i].tech_focalDis,
                    data[i].tech_tranLen,
                    data[i].tech_seg,
                    data[i].sum,
                    data[i].w_V,
                    data[i].w_A,
                    data[i].tech_time,
                ]);
            }
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});

var tecInner = "";
for (let i = 0; i < tecTitle.length; i++) {
    tecInner += "<th>" + tecTitle[i] + "</th>";
}
tecTable.children[0].children[0].innerHTML = tecInner;
for (let i = 0; i < tecInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "";
    for (let j = 0; j < tecTitle.length; j++) {
        inner += "<td>" + tecInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    tecTable.children[1].appendChild(tr);
}

//显示工件信息
var workpieceTable = document.getElementById("workpieceTable");
var workpieceTitle = ["部件编号", "焊缝编号", "片号", "缺陷位置", "缺陷尺寸", "缺陷类型", "评定结果", "备注"];
var workpieceInfomation = [];
var postData;
$.ajax({
    url: '/echo/report',
    type: 'post',
    data: {
        "requestType": "photoData",
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            for (let i = 0; i < data["photo"].length; i++) {
                workpieceInfomation.push([
                    data["photo"][i].w_pipeNum,
                    data["photo"][i].w_weldNum,
                    data["photo"][i].ph_phNum,
                    data["photo"][i].ph_pos,
                    data["photo"][i].ph_size,
                    data["photo"][i].ph_flaw,
                    data["photo"][i].ph_conclusion,
                    data["photo"][i].w_re == 0 ? "" : "第" + (data["photo"][i].w_re + 1) + "次拍片"
                ]);
            }
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});

var workpieceInner = "";
for (let i = 0; i < workpieceTitle.length; i++) {
    workpieceInner += "<th>" + workpieceTitle[i] + "</th>";
    //let th = document.createAttribute("th");
    //th.innerHTML = workpieceTitle[i];
    //document.getElementById("workpieceTable").children[0].children[0].innerHTML(th);
}
workpieceTable.children[0].children[0].innerHTML = workpieceInner;
for (let i = 0; i < workpieceInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "";
    for (let j = 0; j < workpieceTitle.length; j++) {
        inner += "<td>" + workpieceInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    workpieceTable.children[1].appendChild(tr);
}



//显示统计信息
var statTable = document.getElementById("statTable");
var statTitle = [" ", "Ⅰ级", "Ⅱ级", "Ⅲ级", "Ⅳ级"];
var statTitle_v = [];
var statInfomation = [];
var statData = [postData["photo"].length, postData["w_num"]];
document.getElementById("version2").innerHTML = "底片数量（总计）：" + statData[0];
document.getElementById("version3").innerHTML = "工件数量：" + statData[1];
var sum = [];
for (let i = 0; i < postData["photo"].length; i++) {
    if (sum[postData["photo"][i].w_re] == undefined) {
        sum[postData["photo"][i].w_re] = {};
        sum[postData["photo"][i].w_re][postData["photo"][i].ph_evalevel] = 1;
    }
    else sum[postData["photo"][i].w_re][postData["photo"][i].ph_evalevel] += 1
}
for (let i = 0; i < sum.length; i++) {
    statTitle_v[i] = "第" + (i + 1) + "次拍片";
    statInfomation[i] = sum[postData["photo"][i].w_re] == undefined ? [0, 0, 0, 0] : [
        sum[postData["photo"][i].w_re]["Ⅰ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅰ"],
        sum[postData["photo"][i].w_re]["Ⅱ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅱ"],
        sum[postData["photo"][i].w_re]["Ⅲ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅲ"],
        sum[postData["photo"][i].w_re]["Ⅳ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅳ"]
    ];
}
var statInner = "";
for (let i = 0; i < statTitle.length; i++) {
    statInner += "<th>" + statTitle[i] + "</th>";
}
statTable.children[0].children[0].innerHTML = statInner;
for (let i = 0; i < statInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "<th class=\"td-shadows\">" + statTitle_v[i] + "</th>";
    for (let j = 0; j < statTitle.length - 1; j++) {
        inner += "<td>" + statInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    statTable.children[1].appendChild(tr);
}

function OnCommit() {
    var date = new Date();
    $.ajax({
        url: '/echo/report',
        type: 'post',
        data: {
            "requestType": "update",
            "r_company": document.getElementById("r_company").value,
            "r_pname": document.getElementById("r_pname").value,
            "r_position": document.getElementById("r_position").value,
            "r_opportunity": document.getElementById("r_opportunity").value,
            "r_weldM": document.getElementById("r_weldM").value,
            "r_type": document.getElementById("r_type").value,
            "r_equ": document.getElementById("r_equ").value,
            "r_focus": document.getElementById("r_focus").value,
            "r_IQIV": document.getElementById("r_IQIV").value,
            "r_sensitizer": document.getElementById("r_sensitizer").value,
            "r_filmBrand": document.getElementById("r_filmBrand").value,
            "r_filmV": document.getElementById("r_filmV").value,
            "r_devTime": document.getElementById("r_devTime").value,
            "r_devCondition": document.getElementById("r_devCondition").value,
            "r_b": document.getElementById("r_b").value,
            "r_standard": document.getElementById("r_standard").value,
            "r_qua": document.getElementById("r_qua").value,
            "r_heat": document.getElementById("r_heat").value,
            "r_texture": document.getElementById("r_texture").value,
            "r_equid": document.getElementById("r_equid").value,
            "r_develop": document.getElementById("r_develop").value,
            "r_devT": document.getElementById("r_devT").value,
            "r_tlevel": document.getElementById("r_tlevel").value,
            "r_check": document.getElementById("r_check").value,
            "r_scale": document.getElementById("r_scale").value,
            "r_level": document.getElementById("r_level").value,
            "r_person": user.user_id,
            "r_date": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "r_part": user.staff_part,
            "r_remark": document.getElementById("r_remark").value,
            "r_product": document.getElementById("task_product").value
        },
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                alert("提交成功");
                history.back();
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });

}